#include "termostato.h"
#include <QApplication>
#include <QDesktopWidget>
#include <QStyleOptionGraphicsItem>
#include <QGraphicsSceneMouseEvent>
#include <QtCore>
#include <QGraphicsEffect>

Termostato::Termostato()
{
    visible = false;

    img_path = new QString(QApplication::applicationDirPath());

    termostato_img = new QImage(QString("%1/Images/termostato.png").arg(*img_path));
    minimize_icon = new QImage(QString("%1/Images/minimize_icon.png").arg(*img_path));
    setRect(0, 0, termostato_img->width(), termostato_img->height());
    setFlag(ItemIsMovable);

    itemScale = (float) QApplication::desktop()->height() / (float)1080;
    this->setScale(itemScale/4);
    this->setPos(900,300);

    temperatura = 22;
    temperatura2 = 21;

    LCD2 = new Display(Qt::green, QRect(0,0,40,35));
    LCD = new Display(Qt::red, QRect(0,0,55,36));

    LCD->display(temperatura);
    LCD2->display(temperatura2);

    proxy_LCD2 = new QGraphicsProxyWidget;
    proxy_LCD2->setWidget(LCD2);
    proxy_LCD2->setParentItem(this);
    proxy_LCD2->setPos(88,80);

    proxy_LCD = new QGraphicsProxyWidget;
    proxy_LCD->setWidget(LCD);
    proxy_LCD->setParentItem(this);
    proxy_LCD->setPos(81,38);
}

void Termostato::paint( QPainter *p, const QStyleOptionGraphicsItem *option, QWidget * )
{
    p->setRenderHint(QPainter::SmoothPixmapTransform);
    p->drawImage( option->exposedRect, *termostato_img, option->exposedRect, Qt::OrderedAlphaDither );

    if(visible)
    p->drawImage((this->rect().width()- minimize_icon->width()), 0 , *minimize_icon);
}

void Termostato::changeStatus()
{
    visible = !visible;

    if(visible){
        QPropertyAnimation *anim = new QPropertyAnimation(this, "scale");
        anim->setEasingCurve(QEasingCurve::OutCubic);
        anim->setDuration(500);
        anim->setStartValue(itemScale/4);
        anim->setEndValue(itemScale);
        anim->start();
    }
    else{
        QPropertyAnimation *anim = new QPropertyAnimation(this, "scale");
        anim->setEasingCurve(QEasingCurve::OutCubic);
        anim->setDuration(500);
        anim->setStartValue(itemScale);
        anim->setEndValue(itemScale/4);
        anim->start();
    }
}

void Termostato::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    if(event->button() != Qt::LeftButton)
        return;

    if(QRectF(73, 218, 33, 19).contains(event->pos())){
        temperatura--;
        LCD->display(temperatura);
    }
    if(QRectF(112, 218, 33, 19).contains(event->pos())){
        temperatura++;
        LCD->display(temperatura);
    }
    QGraphicsRectItem::mousePressEvent(event);
}

void Termostato::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
    if(!visible)
        changeStatus();
    if(QRectF(this->rect().width()- minimize_icon->width(), 0,
              minimize_icon->width(), minimize_icon->height()).contains(event->pos())){
        changeStatus();
    }
}

